﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class admin_newsedit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["login"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                YqhData obj = new YqhData();
                ddltype.DataSource = obj.FillDataSet("select * from newstype order by tid");
                ddltype.DataTextField = "tname";
                ddltype.DataValueField = "tid";
                ddltype.DataBind();
                if (Request.QueryString["op"] == "e")
                {
                    btnup.Visible = false;
                    DataSet ds;
                    ds = obj.FillDataSet(string.Format("select * from news where nid={0}", Request.QueryString["id"]));
                    ddltype.SelectedValue = ds.Tables[0].Rows[0]["ntypeid"].ToString();
                    txtsubject.Text = ds.Tables[0].Rows[0]["nsubject"].ToString();
                    FCKeditor1.Value = ds.Tables[0].Rows[0]["ncontent"].ToString();
                }
                else if (Request.QueryString["op"] == "new")
                {
                    btnsave.Visible = false;
                }
            }   
        }
    }

    protected void btnup_Click(object sender, EventArgs e)
    {
        YqhData obj = new YqhData();
        string sql = string.Format(@"insert into news(naddname,
                                                      nsubject,
                                                      ntypeid,
                                                      ncontent,
                                                      ndate)
                                                            values
                                                            ('{0}',
                                                             '{1}',
                                                              {2},
                                                             '{3}',
                                                              getdate()
                                                             )",
                                                   Session["login"].ToString(),
                                                   txtsubject.Text,
                                                   ddltype.SelectedValue,
                                                   FCKeditor1.Value.ToString());
        int i = 0;
        try
        {
            i = obj.ExecuteSql(sql);
        }
        catch
        {
            i = 0;
        }

        if (i >= 1)
        {
            Response.Write("<script>alert('发布成功！');window.location.href = 'newslist.aspx';</script>");
        }
        else
        {
            obj.ShowMsg("添加失败！请重新添加！");
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        YqhData obj = new YqhData();
        string sql = string.Format(@"update news 
                                                set nsubject = '{0}',
                                                ncontent = '{1}',
                                                ntypeid = {2}
                                                where nid = {3}",
                                                               txtsubject.Text,
                                                               FCKeditor1.Value,
                                                               ddltype.SelectedValue,
                                                               Request.QueryString["id"]);
        int i = 0;
        try
        {
            i = obj.ExecuteSql(sql);
        }
        catch
        {
            i = 0;
        }

        if (i >= 1)
        {
            Response.Write("<script>alert('保存成功！');window.location.href = 'newslist.aspx';</script>");
        }
        else
        {
            obj.ShowMsg("保存失败！");
        }
    }
}


